package com.vm.adapter;

import java.util.List;
import com.vm.activity.AddressEditActivity;
import com.vm.activity.OrderConfirmActivity;
import com.vm.model.UserAddress;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年1月24日 上午10:01:01]
 * @Version: [v1.0]
 * 
 */

@SuppressLint("InflateParams")
public class AddressManageAdapter extends BaseAdapter {

	private Context mContext;
	private List<UserAddress> list;
	private int isSelect;

	/**
	 * 
	 */
	public AddressManageAdapter(Context context, List<UserAddress> lists,
			int isSelect) {
		this.mContext = context;
		this.list = lists;
		this.isSelect = isSelect;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@SuppressWarnings("unused")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		convertView = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.address_manage_item, null);
			viewHolder = new ViewHolder();

			/**
			 * 获取各个需要显示属性的控件
			 */
			viewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_name);
			viewHolder.tv_phone = (TextView) convertView
					.findViewById(R.id.tv_phone);
			viewHolder.tv_address = (TextView) convertView
					.findViewById(R.id.tv_address);
			viewHolder.btn_edit = (ImageButton) convertView
					.findViewById(R.id.btn_edit);
			viewHolder.ll_address = (LinearLayout) convertView
					.findViewById(R.id.ll_address);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		UserAddress userAddress = list.get(position);
		if (userAddress != null) {
			viewHolder.tv_name.setTag(userAddress);
		}
		if (userAddress.getName() != null) {
			viewHolder.tv_name.setText(userAddress.getName());
		}
		if (userAddress.getTelephone() != null) {
			viewHolder.tv_phone.setText(userAddress.getTelephone());
		}
		if (userAddress.getAddress() != null) {
			if (userAddress.getArea() != null) {
				viewHolder.tv_address.setText(userAddress.getArea()
						+ userAddress.getAddress());
			} else {
				viewHolder.tv_address.setText(userAddress.getAddress());
			}
		}
		viewHolder.btn_edit.setTag(position);
		viewHolder.btn_edit.setOnClickListener(btn_ClickListener);
		if (isSelect == 1) {
			viewHolder.ll_address.setTag(position);
			viewHolder.ll_address.setOnClickListener(item_listener);
		}
		// userAddress = null;

		return convertView;

	}

	OnClickListener btn_ClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(v.getContext(),
					AddressEditActivity.class);
			UserAddress userAddress = list.get(Integer.parseInt(v.getTag()
					.toString()));
			intent.putExtra("order", isSelect);
			intent.putExtra("userAddress", userAddress);
			v.getContext().startActivity(intent);
			((Activity) mContext).finish();
		}
	};
	OnClickListener item_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(v.getContext(),
					OrderConfirmActivity.class);
			UserAddress userAddress = list.get(Integer.parseInt(v.getTag()
					.toString()));
			intent.putExtra("userAddress", userAddress);
			v.getContext().startActivity(intent);
			((Activity) mContext).finish();
		}

	};

	static class ViewHolder {
		TextView tv_name;
		TextView tv_phone;
		TextView tv_address;
		ImageButton btn_edit;
		LinearLayout ll_address;
	}

}
